cc.Class({
    extends: cc.Component,

    properties: {
        personalDataPrefab: cc.Prefab,   //个人信息预制体
        scrollView: cc.Node,             //滑动视图
        content: cc.Node,                //个人信息父物体

        friendData: cc.Prefab,           //好友信息

        rankOfMyInfo: cc.Node,           //排行榜中玩家的信息
        rankOfFriendRank: cc.Label,      //排行榜中玩家的排名
        rankOfHighScore: cc.Label,       //排行榜中玩家最高记录
        myrank:cc.Label, //qian3
    },

    start: function () {

        let self = this;
        var MainFun = {
            onMain(res) {
                window.myOpenid = res.openid;
                console.log(window.myOpenid);
            },
            offMain(res) {
            },

            onRank(res) {
                self.scrollView.active = true;
                self.rankOfMyInfo.active = true;
                let rank = {
                    friend(data) {
                        self.rankOfFriendRank.string = self.newFriendRank;
                        // if(self.newFriendRank < 3){
                        //     self.myrank.node.active = true;
                        //     self.myrank.string = self.newFriendRank
                        // }
                        self.onGetFriendsRankList(data);
                        console.log('前3排名是否显示',self.myrank.node.active,self.newFriendRank)
                    },
                    world(data) {
                        self.rankOfFriendRank.string = self.newWorldRank;
                        // if(self.newWorldRank < 3){
                        //     self.myrank.node.active = true;
                        //     self.myrank.string = self.newWorldRank;
                        // }
                        self.onGetWorldRankList(data);
                        console.log('前3排名是否显示',self.myrank.node.active,self.newWorldRank)
                    }
                }
                rank[res.msg](res.data);
            },
            offRank(res) {
                self.scrollView.active = false;
                self.rankOfMyInfo.active = false;
            },
        }

        //1.监听主域发送的消息
        wx.onMessage(data => {
            let msg = data.msg;
            MainFun[msg](data.data);
        });
    },

   

    /**
     * 将自己和好友进行排名
     */
    onGetFriendsRankList: function (data) {

        let score = data.score;
        this.content.destroyAllChildren();  //清空子节点
        wx.getFriendCloudStorage({
            keyList: ["xxmxj_score"],
            success: res => {
                let friendList = res.data;
                let users = [];
                friendList.forEach(item => {
                    if (item && item.KVDataList && item.KVDataList[0].value > score &&
                        window.myOpenid != item.openid && item.KVDataList[0].value > 0) {
                        users.push(item);
                    }
                });

                let rank = users.length + 1;
                this.setRankScore({
                    rank: rank,
                    score: score
                });

                if (friendList.length > 1) {
                    friendList.sort((a,b)=>{
                        return Number(b.KVDataList[0].value - a.KVDataList[0].value);
                    });
                }
                friendList.forEach((item, index) => {
                    let node = cc.instantiate(this.personalDataPrefab);
                    this.content.addChild(node);
    
                    //调用玩家信息脚本
                    let personalData = node.getComponent('personalData');
                    personalData.property({
                        rank: index + 1,
                        name: item.nickname,
                        url: item.avatarUrl,
                        score: item.KVDataList[0].value,
                        openid:item.openid
                    })
                })
            },
            fail: res => {
            }
        })

    },

    /**
     * 将自己和世界玩家进行排名
     * @param {*} data 
     */
    onGetWorldRankList: function (data) {
        this.setRankScore({
            rank: data.rank,
            score: data.score
        });
        this.content.destroyAllChildren();  //清空子节点
        let list = data.list;
        list.forEach((item, index) => {
            let node = cc.instantiate(this.personalDataPrefab);
            this.content.addChild(node);

            //调用玩家信息脚本
            let personalData = node.getComponent('personalData');
            let score = item.phbRanking; //分数
            let user = item.usersdkList[0];
            personalData.property({
                rank: index + 1,
                name: user.username,
                url: user.userimg,
                score: score,
                openid:user.openid
            })
        })
    },

    setRankScore({rank, score}){
        this.rankOfFriendRank.string = rank;
        this.rankOfHighScore.string = score;
        if(rank <= 3){
            this.myrank.node.active = true;
            this.myrank.string = rank;
        }
    }

});
